#!/usr/bin/perl

# Copyright 2004-2008, Paul Johnson (pjcj@cpan.org)

# This software is free.  It is licensed under the same terms as Perl itself.

# The latest version of this software should be available from my homepage:
# http://www.pjcj.net

use strict;
use warnings;

use Getopt::Long;

my $Options =
{
    dry_run        => 0,
    ignore_failure => 0,
    version        => [],
};

sub get_options
{
    die "Bad option" unless
    GetOptions($Options,  # Store the options in the Options hash.
               qw(
                   dry_run!
                   ignore_failure!
                   version=s
                 ));
    $Options->{version} =
        [ map { ($_, "$_-thr") }
              qw( 5.6.1 5.6.2
                  5.8.0 5.8.1 5.8.2 5.8.3 5.8.4 5.8.5 5.8.6 5.8.7 5.8.8
                  5.10.0 5.11.0 ) ]
        unless @{$Options->{version}};
    $Options->{version} =
        [ grep eval { !system "perl$_ -v" }, @{$Options->{version}} ];
}

sub sys
{
    my ($command) = @_;
    print "$command\n";
    return if $Options->{dry_run};
    my $ret = system $command;
    die "command failed: $?" if $ret && !$Options->{ignore_failure};
}

get_options;
my $command = "@ARGV" or die "Usage: $0 [-v version] command\n";

for my $v (@{$Options->{version}})
{
    my $perl = "perl$v";
    sys "rm -rf t cover_db";
    sys "$perl Makefile.PL";
    sys "make clean";
    sys "$perl Makefile.PL";
    sys "make";
    sys $command;
}
